package editorsvg.editor.herramientas;

import editorsvg.editor.cursores.Cursores;
import editorsvg.editor.documento.Direccion;
import editorsvg.editor.formas.Polilinea;
import editorsvg.editor.herramientas.controles.Asa;
import editorsvg.editor.vista.EditorDocumento;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

/**
 * Herramienta para la creación de polilineas.
 * @author Jorge Berjano
 */
public class HerramientaPolilinea extends HerramientaBase {

    private EditorDocumento editor;
    private Polilinea polilinea;
    private Asa asaInicial;
    private Point2D posicionInicial;

    public HerramientaPolilinea(EditorDocumento editor) {
        this.editor = editor;
    }

    @Override
    public boolean dobleClick(MouseEvent e) {
        finalizar();
        return true;
    }

    private void finalizar() {
        polilinea.finalizar();
        editor.finalizarCreacionForma(polilinea);
        polilinea = null;
        asaInicial = null;
    }

    protected Polilinea crearForma() {
        return new Polilinea();
    }

    @Override
    public boolean pulsado(MouseEvent e) {
        Point2D posicion = editor.getConversor().aUnidadesReales(e.getPoint());
        if (polilinea == null) {
            polilinea = crearForma();
            polilinea.iniciar(posicion);
            asaInicial = new Asa(Direccion.CENTRO, e.getPoint());
            posicionInicial = posicion;
        } else if (asaInicial.contiene(e.getPoint())) {
            if (polilinea.getNumeroPuntos() > 1) {
                polilinea.agregarNodo(posicionInicial);
                finalizar();
            } else {
                polilinea = null;
                asaInicial = null;
            }
        } else {
            polilinea.agregarNodo(posicion);
        }


        return true;
    }

    @Override
    public boolean arrastrando(MouseEvent e) {
        return true;
    }

    @Override
    public boolean moviendo(MouseEvent e) {
        if (polilinea == null) {
            return false;
        }
        Point2D posicion = editor.getConversor().aUnidadesReales(e.getPoint());
        polilinea.siguientePosicion(posicion);
        boolean ratonSobreAsa = asaInicial.contiene(e.getPoint());
        asaInicial.setColorRelleno(ratonSobreAsa ? Color.RED : null);
        return true;
    }

    @Override
    public boolean liberado(MouseEvent e) {
        return false;
    }

    @Override
    public Cursor getCursor() {
        return Cursores.POLILINEA.getCursor();
    }

    @Override
    public void pintarEnPixeles(Graphics2D graphics) {
        if (polilinea == null) {
            return;
        }
        graphics.setColor(Color.BLACK);
        graphics.setXORMode(Color.WHITE);
        graphics.setStroke(new BasicStroke(1));

        if (asaInicial != null) {
            asaInicial.pintar(graphics);
        }
        Shape formaPixels = editor.getConversor().transformarAPixeles(polilinea.getShape());
        graphics.draw(formaPixels);

        graphics.setPaintMode();
    }
}
